#!/bin/bash
set -euo pipefail

push_tag() {
    TAG=$1
    TAG=$1-$(date "+%Y%m%d%H%M%S")-$(echo $((RANDOM%10000)))
    git tag $TAG -m "$TAG"
    git push origin $TAG
    git tag -d $TAG
    CURRENT_BRANCH=$(git symbolic-ref --short HEAD)
    COMMIT=$(git log -n 1 --pretty=format:"%h")
    echo "Pushed tag '$TAG' to branch '$CURRENT_BRANCH' at commit '$COMMIT'."
}

DEFAULT_TAG="trigger-build-bump-patch"

if [ $# -eq 0 ]; then
    TAG=$DEFAULT_TAG
else
    case $1 in
        "minor"|"major"|"patch")
            TAG="trigger-build-bump-$1"
            ;;
        *)
            echo "Invalid tag argument. Usage: $0 [patch (default)|minor|major]"
            exit 1
            ;;
    esac
fi

if [ "$TAG" != "$DEFAULT_TAG" ]; then
    read -p "Are you sure you want to push tag '$TAG'? (y/n): " choice
    if [ "$choice" != "y" ]; then
        echo "Operation canceled."
        exit 0
    fi
fi

push_tag $TAG